﻿using System.Collections.Generic;
using Nuwanda.Common;
using Microsoft.Win32;
using StructureMap;

namespace Nuwanda.TfsSpotlight.TFS
{
    /// <summary>Servers defined for Visual Studio</summary>
    [Author("Bartłomiej Legiędź")]
    [Pluggable("VisualStudioServersRegister")]
    public class VisualStudioServersRegister : IVisualStudioServersRegister
    {
        private readonly string VisualStudio2005Key = @"Software\Microsoft\VisualStudio\8.0\TeamFoundation\Servers";
        private readonly string VisualStudio2008Key = @"Software\Microsoft\VisualStudio\9.0\TeamFoundation\Servers";

        public string[] GetServerNames()
        {
            var servers = new List<string>();

            using (var serversKey = Registry.CurrentUser.OpenSubKey(VisualStudio2005Key))
            {
                if (serversKey != null)
                {
                    foreach (var valueName in serversKey.GetValueNames())
                    {
                        servers.Add(serversKey.GetValue(valueName).ToString());
                    }
                }
            }

            using (var serversKey = Registry.CurrentUser.OpenSubKey(VisualStudio2008Key))
            {
                if (serversKey != null)
                {
                    foreach (var valueName in serversKey.GetValueNames())
                    {
                        servers.Add(serversKey.GetValue(valueName).ToString());
                    }
                }
            }

            return servers.ToArray();
        }
    }
}
